package com.car.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Page<T> implements Serializable {

	private int currentPage = 1;// 当前页
	private int pageSize = 2;// 每页显示的行数

	private int totalPage;// 总页数
	private int totalRow;// 总行数
	private int prevPage;// 上一页
	private int nextPage;// 下一页

	private List<T> list;// 分页数据

	// 分页的查询条件
	private Map<String, Object> mapCond = new HashMap<String, Object>();

	// 翻页url后面的参数 &title=美&id=1

	public String getLinkUrl() {
		String linkUrl = "";
		for (String key : mapCond.keySet()) {
			linkUrl += "&" + key + "=" + mapCond.get(key);
		}

		return linkUrl;
	}

	/**
	 * 页码组
	 */
	private int groupPageCount = 5;// 默认显示5页 1-5
	private List<Integer> groupPage = new ArrayList<Integer>();// 显示组页数

	public Page() {
	}

	public Page(int currentPage) {
		super();
		this.currentPage = currentPage;
		this.mapCond = new HashMap<String, Object>();
	}

	public Page(int currentPage, int pageSize) {
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.mapCond = new HashMap<String, Object>();
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<T> getList() {
		return list;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRow() {
		return totalRow;
	}

	/**
	 * 设置总行数
	 * 
	 * @param totalRow
	 */
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;

		// 计算总页数
		if (this.totalRow % this.pageSize == 0) {
			this.totalPage = this.totalRow / this.pageSize;
		} else {
			this.totalPage = this.totalRow / this.pageSize + 1;
		}
	}

	/**
	 * 判断是否有上一页
	 * 
	 * @return
	 */
	public boolean getHasPrevPage() {

		return this.currentPage - 1 > 0;
	}

	/**
	 * 上一页
	 * 
	 * @param prevPage
	 */
	public int getPrevPage() {
		if (this.currentPage - 1 > 0) {
			this.prevPage = this.currentPage - 1;
		}
		return prevPage;
	}

	/**
	 * 判断是否有下一页
	 * 
	 * @return
	 */
	public boolean getHasNextPage() {
		return this.currentPage + 1 <= this.totalPage;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {

		if (this.currentPage + 1 <= this.totalPage) {
			this.nextPage = this.currentPage + 1;
		}
		return nextPage;
	}

	/**
	 * 显示一组的页数
	 * 
	 * @return
	 */
	public List<Integer> getGroupPage() {

		// 定义起始页 - 结束页
		int startPage = 1, endPage = this.groupPageCount;

		// 在当前页之前几页
		int firstPage = groupPageCount / 2;
		// 在当前页之后几页
		int lastPage = groupPageCount - firstPage;

		// 计算起始页
		if ((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage >= lastPage) {
			startPage = this.currentPage - firstPage + 1;
		} else if ((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage < lastPage) {
			startPage = this.totalPage - this.groupPageCount + 1;
		}

		// 重新计算防止出现为负
		if (startPage < 1) {
			startPage = 1;
		}

		// 计算结束页
		if (this.totalPage - this.currentPage >= lastPage && startPage == 1 && this.totalPage >= this.groupPageCount) {
			endPage = this.groupPageCount;
		} else if (this.totalPage - this.currentPage >= lastPage && this.totalPage >= this.groupPageCount) {
			endPage = this.currentPage + lastPage;
		} else {
			endPage = this.totalPage;
		}

		// 从起始到结束添加集合
		for (int i = startPage; i <= endPage; i++) {
			groupPage.add(i);
		}

		return groupPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public void setGroupPage(List<Integer> groupPage) {
		this.groupPage = groupPage;
	}

	public int getGroupPageCount() {
		return groupPageCount;
	}

	public void setGroupPageCount(int groupPageCount) {
		this.groupPageCount = groupPageCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public Map<String, Object> getMapCond() {
		return mapCond;
	}

	public void setMapCond(Map<String, Object> mapCond) {
		this.mapCond = mapCond;
	}

	@Override
	public String toString() {
		return "Page(currentPage=" + this.currentPage + ",pageSize=" + this.pageSize + ",totalPage=" + this.totalPage
				+ ",totalRow=" + this.totalRow + ",hasPrevPage=" + this.getHasPrevPage() + ",prevPage="
				+ this.getPrevPage() + ",hasNextPage=" + this.getHasNextPage() + ",nextPage=" + this.getNextPage()
				+ ",groupPage=" + this.getGroupPage() + ")";
	}

	public static void main(String[] args) {

	}

}
